package com.studyyoun.android.nonghe.sql;
/**
 * Created by zhaolong on 2017/9/29.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * class infation
 */
public class SqlDBHelper extends SQLiteOpenHelper {

    private static final int DB_VERSION = 10;
    private static final String DB_NAME = "wisdom.db";
    public static final String TABLE_NAME_ART = "t_art";
    public static final String TABLE_NAME_ART_CLASS = "t_art_class";
    public static final String TABLE_NAME_ART_SPE = "t_art_spe";
    public static final String TABLE_NAME_RE_ART_PARISE = "t_re_art_parise";
    /**
     * 对应收藏用户
     */
    public static final String TABLE_NAME_RE_ART_Collect = "t_re_art_user_collect";

    /**
     * 推送消息
     */
    public static final String TABLE_NAME_JPUSH = "t_com_jpush_msg";

    /**
     * 私有的构造函数，只能自己使用，防止绕过同步方法生成多个实例，
     *
     * @param context
     */
    private SqlDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    /*私有的静态对象，为整个应用程序提供一个sqlite操作的静态实例，并保证只能通过下面的静态方法getHelper(Context context)获得，
     * 防止使用时绕过同步方法改变它*/
    private static SqlDBHelper instance;//这里主要解决死锁问题,是static就能解决死锁问题


    /**
     * 为应用程序提供一个单一的入口，保证应用程序使用同一个对象操作数据库，不会因为对象不同而使同步方法失效
     *
     * @param context 上下文
     * @return instance
     */
    public static SqlDBHelper getHelper(Context context) {
        if (instance == null)
            instance = new SqlDBHelper(context);
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        // create table Orders(Id integer primary key, CustomName text, OrderPrice integer, Country text);
        String sql = "create table if not exists " + TABLE_NAME_ART + " (Id integer primary key autoincrement,art_id integer, art_name text,art_img text, art_flag text, art_content text,art_creat_time varchar(50),art_parise_count integer,art_read_count integer,art_is_top integer,art_is_recommend integer)";
        sqLiteDatabase.execSQL(sql);

        String artClassSql = "create table if not exists " + TABLE_NAME_ART_CLASS + " (Id integer primary key autoincrement,art_clas_id integer,art_id integer, art_clas_name text,art_clas_content text, art_clas_creat_time text, art_clas_creat_image text,art_clas_creat_uid varchar(50))";
        sqLiteDatabase.execSQL(artClassSql);

        String artSpeSql = "create table if not exists " + TABLE_NAME_ART_SPE + " (Id integer primary key autoincrement,art_spe_id integer,art_id integer, art_spe_name text,art_spe_content text, art_spe_creat_time text, art_spe_creat_image text,art_spe_creat_uid varchar(50))";
        sqLiteDatabase.execSQL(artSpeSql);

        String artAndPariseSpeSql = "create table if not exists " + TABLE_NAME_RE_ART_PARISE + " (Id integer primary key autoincrement,art_id integer,user_id integer, creat_time varchar(50))";
        sqLiteDatabase.execSQL(artAndPariseSpeSql);

        String artAndCollectSpeSql = "create table if not exists " + TABLE_NAME_RE_ART_Collect + " (Id integer primary key autoincrement,art_id integer,user_id integer, creat_time varchar(50))";
        sqLiteDatabase.execSQL(artAndCollectSpeSql);

        String jpushSpeSql = "create table if not exists " + TABLE_NAME_JPUSH + " (Id integer primary key autoincrement,type_id integer,type varchar(50),title varchar(200),content varchar(500),msg_id varchar(100))";
        sqLiteDatabase.execSQL(jpushSpeSql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        String sql = "DROP TABLE IF EXISTS " + TABLE_NAME_ART;
        sqLiteDatabase.execSQL(sql);

        String artClassSql = "DROP TABLE IF EXISTS " + TABLE_NAME_ART_CLASS;
        sqLiteDatabase.execSQL(artClassSql);

        String artSpeSql = "DROP TABLE IF EXISTS " + TABLE_NAME_ART_SPE;
        sqLiteDatabase.execSQL(artSpeSql);

        String artAndPariseSpeSql = "DROP TABLE IF EXISTS " + TABLE_NAME_RE_ART_PARISE;
        sqLiteDatabase.execSQL(artAndPariseSpeSql);

        String artAndCollectSpeSql = "DROP TABLE IF EXISTS " + TABLE_NAME_RE_ART_Collect;
        sqLiteDatabase.execSQL(artAndCollectSpeSql);

        String jpushSql = "DROP TABLE IF EXISTS " + TABLE_NAME_JPUSH;
        sqLiteDatabase.execSQL(jpushSql);

        onCreate(sqLiteDatabase);
    }

}
